// https://github.com/jantimon/html-webpack-plugin#events

const fs = require('fs')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const rem = require('../../config/rem.config')
const vconsole = require('../../config/vconsole.config')
const remInjection = require('./remInjection')
const vconsoleInjection = require('./vconsoleInjection')
const tips = require('../../bin/tips')
const wxconfig = require('../../config/wx-agent.config')

class HtmlWebpackExtensionPlugin {
  apply(compiler) {
    compiler.hooks.compilation.tap(
      'HtmlWebpackPluginExtension',
      (compilation) => {
        HtmlWebpackPlugin.getHooks(compilation).alterAssetTagGroups.tapAsync(
          'HtmlWebpackPluginExtension',
          (data, cb) => {
            // cdn注入
            let cdn = data.plugin.options.templateParameters.cdn
            for (let i = 0; i < cdn.length; i++) {
              data.headTags.push({
                tagName: cdn[i].tagName,
                voidTag: false,
                attributes: {
                  src: cdn[i].url
                }
              })
            }
            // 设置 rem
            if (rem.size) {
              const remSize = parseInt(rem.size)
              if (isNaN(remSize)) {
                tips.fail(
                  'size is invalid, check the config in config/rem.config.js!'
                )
              } else {
                const remTransformer = {
                  tagName: 'script',
                  voidTag: false,
                  innerHTML: remInjection
                }
                data.headTags.unshift(remTransformer)
              }
            }
            // 启用vconsole
            if (vconsole.enable) {
              // 开发环境下
              if (process.env.NODE_ENV === 'development') {
                const urlReg = /(https?|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/
                const isLocalFile = !urlReg.test(vconsole.resource)
                const isExistVconsoleFile = isLocalFile
                  ? fs.existsSync(vconsole.resource)
                  : false
                // 非本地文件并且不存在vconsole文件
                if (isLocalFile && !isExistVconsoleFile) {
                  tips.fail(
                    'vconsole.min.js is not found so that vconsole can not take effect!'
                  )
                } else {
                  const vconsoleInstance = {
                    tagName: 'script',
                    voidTag: false,
                    innerHTML: vconsoleInjection
                  }
                  data.headTags.push(vconsoleInstance)
                }
              }
            }
            // 启用wx接入配置
            if (wxconfig.enable) {
              let wxConsoleInstance = wxconfig.resources
              for (let i = 0; i < wxConsoleInstance.length; i++) {
                data.headTags.push({
                  tagName: 'script',
                  voidTag: false,
                  attributes: {
                    src: wxConsoleInstance[i]
                  }
                })
              }
            }
            cb(null, data)
          }
        )
      }
    )
  }
}

module.exports = HtmlWebpackExtensionPlugin
